//
// Created by zx208 on 2024/10/16.
//

#include "../SequenceList.h"

// 编写算法，将两个非递减有序顺序表A和B合并成一个新的非递减有序顺序表C，已知顺序表A和B的元素个数分别为m和n。

SqList* Merge(SqList *L1, SqList *L2, SqList *L3){
    // 判断是否有空顺序表
    if(!L1) return L2;
    if(!L2) return L1;

    // 进行合并
    int index1 = 0, index2 = 0, index3 = 0;
    while(index1 < L1->length && index2 < L2->length){
        if(L1->elem[index1] < L2->elem[index2]) L3->elem[index3++] = L1->elem[index1++];
        else L3->elem[index3++] = L2->elem[index2++];
        L3->length++;
    }

    // 可能会有某个顺序表剩余元素
    while(index1 < L1->length) L3->elem[index3++] = L1->elem[index1++], L3->length++;
    while(index2 < L2->length) L3->elem[index3++] = L2->elem[index2++], L3->length++;

    return L3;
}

int main() {
    SqList L1, L2, L3;
    int a[] = {7, 8, 9}, b[] = {4, 5, 6}, c[] = {0, 0, 0, 0, 0, 0};
    L1.length = sizeof a / sizeof( int);
    L2.length = sizeof b / sizeof( int);
    L3.length = 0;
    L1.elem = a;
    L2.elem = b;
    L3.elem = c;
    L3 = *Merge(&L1, &L2, &L3);
    for (int i = 0; i < L3.length; ++i) {
        printf("%d", L3.elem[i]);
    }
    return 0;
}
